package cc.toprank.syncdatatask.util;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class FieldHelper {
	private static FieldHelper instance = new FieldHelper();

	private FieldHelper() {

	}

	public synchronized static FieldHelper getInstance() {
		if (instance == null) {
			instance = new FieldHelper();
		}
		return instance;
	}

	// 根据字段类型，返回对应的Object值
	public Object getFormattedObjectFieledValue(String fieledValue, String fieldType)
			throws ParseException, UnsupportedEncodingException {
		if (fieledValue == null) {
			return null;
		}
		
		if ("null".equalsIgnoreCase(fieledValue)) {
			return null;
		}

		if ("".equals(fieledValue) && "varchar,longtext,tinytext,text,mediumtext".indexOf(fieldType) < 0) {
			return null;
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

		if ("varchar".equalsIgnoreCase(fieldType))
			return new String(fieledValue);
		if ("bigint".equalsIgnoreCase(fieldType))
			return new Long(fieledValue);
		if ("longtext".equalsIgnoreCase(fieldType))
			return fieledValue;
		if ("datetime".equalsIgnoreCase(fieldType)) {
			return sdf.parse(fieledValue);
		}

		if ("int".equalsIgnoreCase(fieldType))
			return new Integer(fieledValue);
		if ("tinyint".equalsIgnoreCase(fieldType))
			return new Byte(fieledValue);
		if ("decimal".equalsIgnoreCase(fieldType))
			return new BigDecimal(fieledValue);
		if ("double".equalsIgnoreCase(fieldType))
			return new Double(fieledValue);
		if ("bit".equalsIgnoreCase(fieldType))
			return new Integer(fieledValue);
		if ("tinytext".equalsIgnoreCase(fieldType))
			return fieledValue;
		if ("text".equalsIgnoreCase(fieldType))
			return fieledValue;
		if ("date".equalsIgnoreCase(fieldType))
			return sdf.parse(fieledValue);
		if ("char".equalsIgnoreCase(fieldType))
			return fieledValue;
		if ("timestamp".equalsIgnoreCase(fieldType))
			return sdf.parse(fieledValue);
		// if ("set".equalsIgnoreCase(fieldType)) return "'" + FieledValue +
		// "'";
		// if ("enum".equalsIgnoreCase(fieldType)) return "'" + FieledValue +
		// "'";
		if ("longblob".equalsIgnoreCase(fieldType))
			return new ByteArrayInputStream(fieledValue.getBytes("utf-8"), 0, fieledValue.getBytes("utf-8").length);
		if ("mediumtext".equalsIgnoreCase(fieldType))
			return fieledValue;
		if ("smallint".equalsIgnoreCase(fieldType))
			return new Short(fieledValue);
		if ("blob".equalsIgnoreCase(fieldType))
			return new ByteArrayInputStream(fieledValue.getBytes("utf-8"), 0, fieledValue.getBytes("utf-8").length);
		// return fieldValue.
		if ("float".equalsIgnoreCase(fieldType))
			return new Float(fieledValue);
		if ("time".equalsIgnoreCase(fieldType))
			return sdf.parse(fieledValue);
		if ("datetime".equalsIgnoreCase(fieldType))
			return sdf.parse(fieledValue);
		if ("timestamp".equalsIgnoreCase(fieldType))
			return sdf.parse(fieledValue);

		return fieledValue;
	}

}
